var pid="";
var restTable = function(id,name){
	pid=id;
	$("#channelTitle").html(name);
	$('.channelChildTable').dataTable().fnDraw(true);
};
var channel = function(){
	/**
	 * 初始化左侧导航
	 */
	var handleNav = function(){
		$.ajax({
			url :"../admin/branch/get",
			type : "get",
			dataType : "json",
			async:false,
			success:function(result){
				if(result.success){
					var str = "";
					if(result.obj!=undefined){
						$.each(result.obj,function(i){
							str+="<option value='"+this.id+"'>"+this.name+"</option>";
						});
					}else{
						str+="<option value='0'>暂无可用平台</option>";
					}
					$("#branchList").html(str);
					$("#branchList").on("change",function(){
						$('.channelChildTable').dataTable().fnDraw(true);
					});
				}else{
				}
			},error:function(a,b,c){
				if(a.readyState==4){
    				window.location.href="../admin/loginpage";
    			}else{
    				toastr.error("系统异常！");
    			}
			}
		});
		
		$.ajax({
			url :"../admin/channel/parent",
			type : "get",
			dataType : "json",
			async:false,
			success:function(result){
				if(result.success){
					var str = "";
					$.each(result.obj,function(i){
						str+="<li><a href='javascript:void(0);' onclick='restTable(\""+this.id+"\",\""+this.name+"\")'><i class='fa fa-folder'></i>"+this.name+"</a></li>";
					});
					$("#channelParentList").html(str);
					pid=result.obj[0].id;
					$("#channelTitle").html(result.obj[0].name);
					handleTable();
				}else{
				}
			},error:function(a,b,c){
				if(a.readyState==4){
    				window.location.href="../admin/loginpage";
    			}else{
    				toastr.error("系统异常！");
    			}
			}
		});
	};
	/**
	 * 初始化栏目表格
	 */
	var handleTable = function(){
		var ctable = $('.channelChildTable');
		var ccTable = ctable.dataTable({
	        "lengthMenu": [
	               [10,20,30,40,50,100],
	               [10,20,30,40,50,100]
	        ],
	        "pageLength": 10,
	        "language": {
	        	"processing": "数据加载中...",
	            "emptyTable": "暂无数据",
	            "info": "第_START_到_END_条记录,共_TOTAL_条记录",
	            "infoEmpty": "没有找到记录",
	            "infoFiltered": "",
	            "lengthMenu": "每页显示 _MENU_ 条记录",
	            "search": "搜索:",
	            "zeroRecords": "未找到匹配记录",
	            "paginate": {
	                "previous":"上一页",
	                "next": "下一页",
	                "last": "末页",
	                "first": "首页"
	            }
	        },
	        "columnDefs": [
	            {'orderable': false,"data":"id",'targets': [0],'Sortable': false,"visible": false},
	            {'orderable': false,"data":"name",'targets': [1],'Sortable': false,"title":"栏目名称"},
	            {'orderable': false,"data":"key",'targets': [2],"title":"栏目key",
	                "render":function(data, type, full){
	                	if(full.key===""){
	                		return"<span style='color:red;'>无</span>";
	                    }else{
	                    	return full.key;
	                    }
	                }
	            }, {'orderable': false,"data":"state",'targets': [3],"title":"状态",
	                "render":function(data, type, full){
	                	if(full.state==0){
	                		return"<span class='label label-primary'>可用</span>";
	                    }else if(full.state==1){
	                    	return"<span class='label label-danger'>暂停使用</span>";
	                    }else if(full.state==2){
	                    	return"<span class='label label-info'>待审核</span>";
	                    }else if(full.state==3){
	                    	return"<span class='label label-warning'>未通过</span>";
	                    }else if(full.state==9){
		          			return"<span class='label label-danger'>暂无可用平台</span>";
		          		}else{
		          			return"<span class='label label-danger'>暂未申请</span>";
		          		}
	                }
	            },
	           {'orderable': false,'targets': [4],"title":"操作",
	          	 "render":function(data,type,full){
	          		 // -1、删除  0、可用  1、禁用 2、审核中 3、审核不通过  
	          		 if(full.state!=9){
		          		 var btn = "<button class='btn btn-success' type='button' id='applyBtn'><i class='fa fa-check'></i>提交申请</button>";
		          		 if(full.state==0){
		          			 btn="<button class='btn btn-warning' type='button' id='stopBtn'><i class='fa fa-ban'></i>暂停使用</button>";
		          		 }else if(full.state==1){
		          			 btn="<button class='btn btn-primary' type='button' id='recoveryBtn'><i class='fa fa-check'></i>恢复使用</button>";
		          		 }else if(full.state==2){
		          			 btn="<button class='btn btn-warning' type='button' id='cancelBtn'><i class='fa fa-warning'></i>取消申请</button>";
		          		 }else if(full.state==3){
		          			btn ="&nbsp;<button class='btn btn-info' type='button' id='reloadBtn'><i class='fa fa-refresh'></i> 重新申请</button>";
		          		 }
	          		 }else{
	          			 btn="<span class='label label-danger'>请申请平台后操作</span>";
	          		 }
	          		 return btn;
	          	 }
	           },
	        ],
	        "bAutoWidth":false,
	        "bProcessing": true, // 是否显示取数据时的那个等待提示
		    "bServerSide": true,//这个用来指明是通过服务端来取数据
	        "sAjaxSource":"../admin/channel/child",//请求的地址
	        "fnServerData": function(sSource,aoData,fnCallback){				  // 获取数据的处理函数
	        	$.ajax({
	        		url:sSource,
	        		type : "post",
	        		data:{
	        			"bid":$("#branchList").val(),
	        			"id":pid,
	        			"aoData":JSON.stringify(aoData)
	        		},
	        		dataType : "json",
	        		async:false,
	        		success:function(result){
	        			result=eval("("+result+")");
	        			fnCallback(result);
	        		},error:function(a,b,c){
	        			if(a.readyState==4){
		    				window.location.href="../admin/loginpage";
		    			}else{
		    				toastr.error("系统异常！");
		    			}
	        		}
	        	});
	        } 
	    });
		ctable.on("click","#sqqxBtn",function(e){
			e.preventDefault();	
	    	var nRow = $(this).parents('tr');
	    	var dData = ccTable.fnGetData(nRow);
			 $('#sqqxModal').modal()
		});
		
		//申请栏目权限
		ctable.on("click","#applyBtn",function(e){
			e.preventDefault();	
	    	var nRow = $(this).parents('tr');
	    	var dData = ccTable.fnGetData(nRow);
	    	$.ajax({
	    		url :"../admin/apply",
	    		type : "post",
	    		dataType : "json",
	    		data:{
	    			bid:$("#branchList").val(),
	    			key:dData.key,
	    			name:dData.name
	    		},
	    		success:function(result){
	    			if(result.success){
	    				toastr.success('申请成功，等待管理员审核！');
	    			}else{
	    				toastr.error("申请失败！");
	    			}
	    			ccTable.fnDraw(true);
	    		},error:function(a,b,c){
	    			if(a.readyState==4){
	    				window.location.href="../admin/loginpage";
	    			}else{
	    				toastr.error("系统异常！");
	    			}
	    		}
	    	});
		});
		//暂停使用
		ctable.on("click","#stopBtn",function(e){
			e.preventDefault();	
	    	var nRow = $(this).parents('tr');
	    	var dData = ccTable.fnGetData(nRow);
	    	$.ajax({
	    		url :"../admin/channel/stop",
	    		type : "post",
	    		dataType : "json",
	    		data:{
	    			bid:$("#branchList").val(),
	    			key:dData.key
	    		},
	    		success:function(result){
	    			if(result.success){
	    				toastr.success('暂停使用成功！');
	    				ccTable.fnDraw(true);
	    			}else{
	    				toastr.error("暂停使用失败！");
	    			}
	    		},error:function(a,b,c){
	    			if(a.readyState==4){
	    				window.location.href="../admin/loginpage";
	    			}else{
	    				toastr.error("系统异常！");
	    			}
	    		}
	    	});
		});
		//恢复使用
		ctable.on("click","#recoveryBtn",function(e){
			e.preventDefault();	
	    	var nRow = $(this).parents('tr');
	    	var dData = ccTable.fnGetData(nRow);
	    	$.ajax({
	    		url :"../admin/channel/recovery",
	    		type : "post",
	    		dataType : "json",
	    		data:{
	    			bid:$("#branchList").val(),
	    			key:dData.key
	    		},
	    		success:function(result){
	    			if(result.success){
	    				toastr.success('恢复使用成功！');
	    				ccTable.fnDraw(true);
	    			}else{
	    				toastr.error("恢复使用失败！");
	    			}
	    		},error:function(a,b,c){
	    			if(a.readyState==4){
	    				window.location.href="../admin/loginpage";
	    			}else{
	    				toastr.error("系统异常！");
	    			}
	    		}
	    	});
		});
		//取消申请
		ctable.on("click","#cancelBtn",function(e){
			e.preventDefault();	
	    	var nRow = $(this).parents('tr');
	    	var dData = ccTable.fnGetData(nRow);
	    	$.ajax({
	    		url :"../admin/channel/cancel",
	    		type : "post",
	    		dataType : "json",
	    		data:{
	    			bid:$("#branchList").val(),
	    			key:dData.key
	    		},
	    		success:function(result){
	    			if(result.success){
	    				toastr.success('取消申请成功！');
	    				ccTable.fnDraw(true);
	    			}else{
	    				toastr.error("取消申请失败！");
	    			}
	    		},error:function(a,b,c){
	    			if(a.readyState==4){
	    				window.location.href="../admin/loginpage";
	    			}else{
	    				toastr.error("系统异常！");
	    			}
	    		}
	    	});
		});
		//重新申请
		ctable.on("click","#reloadBtn",function(e){
			e.preventDefault();	
	    	var nRow = $(this).parents('tr');
	    	var dData = ccTable.fnGetData(nRow);
	    	$.ajax({
	    		url :"../admin/channel/reload",
	    		type : "post",
	    		dataType : "json",
	    		data:{
	    			bid:$("#branchList").val(),
	    			key:dData.key
	    		},
	    		success:function(result){
	    			if(result.success){
	    				toastr.success('重新申请成功！');
	    				ccTable.fnDraw(true);
	    			}else{
	    				toastr.error("重新申请失败！");
	    			}
	    		},error:function(a,b,c){
	    			if(a.readyState==4){
	    				window.location.href="../admin/loginpage";
	    			}else{
	    				toastr.error("系统异常！");
	    			}
	    		}
	    	});
		});
	};
	
	return {
		init : function(){
			handleNav();
		},restTable1:function(id){
			handleTable(id);
		}
	};
}();
channel.init();
